﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;
using System.Collections.Generic;

namespace UI
{
    public class AcLoginAwardItem : MonoBehaviour
    {
        public UILabel labTitle;
        public UIScrollView scrollView;
        public SimpleGrid simpleGrid;
        public UISprite titleLogo;
        public GameObject btnGet;
        public GameObject imgGetted;
        public GameObject imgNotReached;
        public GameObject namePref;
        public GameObject backReciever;
        public UILabel progress;
        public UILabel proLabel;
        public GameObject commonIcon;
        public delegate void UpdateList(List<ActivityItem> list);
        public UpdateList m_callback;

        public delegate void AddRedNum();
        public AddRedNum m_addNum;

        int oneOrAll;
        string activityId;
        int itemIndex;
        List<BaseResource> getRes = new List<BaseResource>();
        public void SetActivityItem(ActivityItem activityItem, string acId)
        {
            labTitle.text = string.Format(TableTextBlo.GetText("keep_login_x_day"), activityItem.value);
            if(proLabel != null)
                proLabel.text = TableTextBlo.GetText("schedule") + "：";
            activityId = acId;
            itemIndex = activityItem.index;
            if (activityItem.index % 3 == 1)
                titleLogo.spriteName = "UI_item_title_xing_blue";
            else if (activityItem.index % 3 == 2)
                titleLogo.spriteName = "UI_item_title_xing_zi";
            else
                titleLogo.spriteName = "UI_item_title_xing_green";
            //奖励
            foreach (var item in simpleGrid.childs)
            {
                Destroy(item);
            }
            simpleGrid.childs.Clear();

            float scale = 0.9f; 
            for (int i = 0; i < activityItem.rewardList.Count; i++)
            {
                //icon
                GameObject obj = Instantiate(commonIcon);
                obj.SetActive(true);
                simpleGrid.AddChild(obj);
                obj.transform.localScale = new Vector3(scale, scale, scale);
                obj.GetComponent<CommonIcon>().SetData(activityItem.rewardList[i].id, true, activityItem.rewardList[i].num);

                //name
                //GameObject name = Instantiate(namePref);
                //name.SetActive(true);
                //name.GetComponent<UILabel>().text = TableTextBlo.GetText(CommonMethod.GetObjectNameTextId(activityItem.rewardList[i].id));
                //name.transform.parent = obj.transform;
                //name.transform.localPosition = new Vector3(0, -57, 0);
                //name.transform.localScale = Vector3.one;
            }
            oneOrAll = activityItem.pickOneOrGetAll;
            getRes = activityItem.rewardList;
            progress.text = activityItem.left.ToString() + "/" + activityItem.total.ToString();
            if (activityItem.rewardList.Count <= 3)
            {
                backReciever.GetComponent<BoxCollider>().enabled = false;
            }

            //领奖状态
            imgNotReached.SetActive(activityItem.status == 0);
            btnGet.SetActive(activityItem.status == 1);
            imgGetted.SetActive(activityItem.status == 2);
            if(activityItem.status == 1 && m_addNum != null)
                m_addNum();
        }

        public void OnBtnGet()
        {
            if (oneOrAll == 1) 
            {
                CSActivityReward msg = new CSActivityReward();
                msg.activityId = this.activityId;
                msg.index = itemIndex;
                msg.getResources.AddRange(getRes);
                NetAPI.SendHttp(OpDefine.CSActivityReward, msg, GetRewardCallback);
            }
            else if(oneOrAll == 0)
            {
                PageManager.Instance.OpenPage("SelectAcRewardPage","");
                SelectAcRewardPage reward = PageManager.Instance.GetDictPage("SelectAcRewardPage") as SelectAcRewardPage;
                reward.m_click = this.SelectAcAward;
                reward.SetReward(getRes);
            }
        }

        void SelectAcAward(BaseResource item)
        {
            CSActivityReward msg = new CSActivityReward();
            msg.activityId = this.activityId;
            msg.index = itemIndex;
            for (int i = 0; i < getRes.Count; i++)
            {
                if (getRes[i].id == item.id)
                {
                    msg.getResources.Add(getRes[i]);
                    break;
                }
            }
            NetAPI.SendHttp(OpDefine.CSActivityReward, msg, GetRewardCallback);
        }

        void GetRewardCallback(NetWork.Packets.Packet packet, bool success)
        {
            if (success)
            {
                SCActivityReward msg = packet.kBody as SCActivityReward;
                StaticData.playerData.AddResultObj(msg.get);
                CommonMethod.ShowGetObjs(msg.get);
                m_callback(msg.itemList);
            }
        }
    }
}

